import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class ClientFrame extends JFrame {
	private static final long serialVersionUID = 4274508423510284389L;

	private JPanel contentPane;
	private JTextField textField;
	private JTextPane textPane;
	private JButton btnSend;
	private Style boldStyle, blackStyle, italicStyle;
	private JScrollPane scrollPane_1, scrollPane;
	private JTextPane textPane_1;

	/**
	 * Launch the application.
	 */
	// public static void main(String[] args) {
	// EventQueue.invokeLater(new Runnable() {
	// public void run() {
	// try {
	// ClientFrame frame = new ClientFrame();
	// frame.setVisible(true);
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }
	// });
	// }

	/**
	 * Create the frame.
	 */
	public ClientFrame(String nickname) {
		this.setTitle("Nickname: " + nickname);

		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 492, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		textField = new JTextField();
		textField.setBounds(6, 244, 375, 28);
		contentPane.add(textField);
		textField.setColumns(10);

		btnSend = new JButton("Send");
		btnSend.setBounds(393, 245, 93, 29);
		contentPane.add(btnSend);

		getRootPane().setDefaultButton(btnSend);

		JPanel panel = new JPanel();
		panel.setBounds(6, 6, 480, 227);
		contentPane.add(panel);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[] { 180, 240 };
		gbl_panel.rowHeights = new int[] { 227 };
		gbl_panel.columnWeights = new double[] { 0.0, 1.0 };
		gbl_panel.rowWeights = new double[] { 0.0 };
		panel.setLayout(gbl_panel);

		scrollPane_1 = new JScrollPane();
		scrollPane_1.setViewportBorder(new TitledBorder(null,
				"Connected users", TitledBorder.LEADING, TitledBorder.TOP,
				null, null));
		GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();
		gbc_scrollPane_1.fill = GridBagConstraints.BOTH;
		gbc_scrollPane_1.insets = new Insets(0, 0, 0, 5);
		gbc_scrollPane_1.gridx = 0;
		gbc_scrollPane_1.gridy = 0;
		panel.add(scrollPane_1, gbc_scrollPane_1);

		textPane_1 = new JTextPane();
		scrollPane_1.setViewportView(textPane_1);
		textPane_1.setEditable(false);

		scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 1;
		gbc_scrollPane.gridy = 0;
		panel.add(scrollPane, gbc_scrollPane);

		textPane = new JTextPane();
		scrollPane.setViewportView(textPane);
		textPane.setEditable(false);
		boldStyle = textPane.addStyle("Bold style", null);
		italicStyle = textPane.addStyle("Italic style", null);
		blackStyle = textPane.addStyle("Black style", null);

		StyleConstants.setBold(boldStyle, true);
		StyleConstants.setItalic(italicStyle, true);
		StyleConstants.setForeground(blackStyle, Color.BLACK);
	}

	public void setSendListener(ActionListener listener) {
		btnSend.addActionListener(listener);
	}

	public String getMessageText() {
		String messageText = textField.getText();
		textField.setText("");
		return messageText;
	}

	public void addMessage(Message m, boolean isMine) {
		StyledDocument doc = textPane.getStyledDocument();
		Style s;
		if (m.getOperation() == OperationType.INFORMATION) {
			s = italicStyle;
			try {
				doc.insertString(doc.getLength(), "[" + m.getText() + "]"
						+ "\n", s);
			} catch (BadLocationException e) {
			}
		} else if (m.getOperation() == OperationType.CLIENTLIST) {

			// aggiorna la lista dei client connessi
			textPane_1.setText(m.getText());
		} else {
			if (isMine) {
				s = boldStyle;
			} else {
				s = blackStyle;
			}
			try {
				doc.insertString(doc.getLength(), m.toString() + "\n", s);
			} catch (BadLocationException e) {
			}
		}
		scrollPane.getVerticalScrollBar().setValue(scrollPane.getVerticalScrollBar().getMaximum());
	}

}
